package io.renren.modules.member.controller;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.modules.member.entity.TStoreMemberAccountDetailEntity;
import io.renren.modules.member.entity.TStoreMemberRechargeRecordEntity;
import io.renren.modules.member.entity.TStoreMemberRuleEntity;
import io.renren.modules.member.entity.vo.MemberRechargeRecordVo;
import io.renren.modules.member.service.TStoreMemberAccountDetailService;
import io.renren.modules.member.service.TStoreMemberRechargeRecordService;
import io.renren.modules.member.service.TStoreMemberRuleService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-27 17:00:49
 */
@RestController
@RequestMapping("store/tstorememberrechargerecord")
public class TStoreMemberRechargeRecordController extends AbstractController {
    @Autowired
    private TStoreMemberRechargeRecordService tStoreMemberRechargeRecordService;
    @Autowired
    private TStoreMemberRuleService tStoreMemberRuleService;
    @Autowired
    private TStoreMemberAccountDetailService tStoreMemberAccountDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorememberrechargerecord:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tStoreMemberRechargeRecordService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listRechargeRecords")
    @RequiresPermissions("store:tstorememberrechargerecord:list")
    public R listRechargeRecords(@RequestParam Map<String, Object> params){
        Page<MemberRechargeRecordVo> voPage = tStoreMemberRechargeRecordService.listRechargeRecords(params);
        return R.ok().put("data",voPage);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstorememberrechargerecord:info")
    public R info(@PathVariable("id") Long id){
		TStoreMemberRechargeRecordEntity tStoreMemberRechargeRecord = tStoreMemberRechargeRecordService.getById(id);

        return R.ok().put("tStoreMemberRechargeRecord", tStoreMemberRechargeRecord);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorememberrechargerecord:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TStoreMemberRechargeRecordEntity tStoreMemberRechargeRecord){
        //保存会员充值记录表
        tStoreMemberRechargeRecord.setSellerId(getSeller());
        tStoreMemberRechargeRecord.setSerialNumber(String.valueOf(DateUtil.currentSeconds()));
        tStoreMemberRechargeRecord.setCreateTime(new Date());
        tStoreMemberRechargeRecord.setCreateUserId(getUserId());
        List<TStoreMemberRechargeRecordEntity> list = new ArrayList<>();
        //有营销人员
        if (tStoreMemberRechargeRecord.getSalemanList().size() != 0){
            for (Long saleman : tStoreMemberRechargeRecord.getSalemanList()){
                tStoreMemberRechargeRecord.setSalesman(saleman);
                TStoreMemberRechargeRecordEntity entity = new TStoreMemberRechargeRecordEntity();
                BeanUtil.copyProperties(tStoreMemberRechargeRecord,entity);
                list.add(entity);
            }
            tStoreMemberRechargeRecordService.saveBatch(list);
        }
        //无营销人员
        else {
            tStoreMemberRechargeRecord.setSalesman(null);
            tStoreMemberRechargeRecordService.save(tStoreMemberRechargeRecord);
        }

        //查出积分规则
        TStoreMemberRuleEntity rule = tStoreMemberRuleService.getOne(
                new QueryWrapper<TStoreMemberRuleEntity>()
                .select("exchange_amount", "exchange_integral")
                .eq("seller_id", getSeller())
                .eq("member_type_id",tStoreMemberRechargeRecord.getMemberTypeId()));
        //金额和积分的兑换率
        double d = rule.getExchangeIntegral()/rule.getExchangeAmount();

        //赠送金额+充值金额
        double amount = tStoreMemberRechargeRecord.getGiveAmout()+tStoreMemberRechargeRecord.getRechargeAmount();
        //积分
        double integral = amount*d;
        Map<String,Object> params = new HashMap<>();
        params.put("sellerId",getSeller());
        params.put("memberId",tStoreMemberRechargeRecord.getMemberId());
        params.put("integral",integral);
        params.put("amount",amount);
        tStoreMemberAccountDetailService.updateMemberAccount(params);

        //保存员工提成表

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorememberrechargerecord:update")
    public R update(@RequestBody TStoreMemberRechargeRecordEntity tStoreMemberRechargeRecord){
		tStoreMemberRechargeRecordService.updateById(tStoreMemberRechargeRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorememberrechargerecord:delete")
    public R delete(@RequestBody Long[] ids){
		tStoreMemberRechargeRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
